/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dc;

/**
 *
 * @author GustavoKatel_
 */
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.widgets.*;
import tools.prop;

public class Start{
 
    public static final prop web_config = new prop("web.conf");
    public static final prop main_config = new prop("main.conf");
    private static Browser browser;
    public static String home = "http://127.0.0.1:3456/home";//"http://127.0.0.1/dcsis_v2.1/erro.php?q=daemon";//"http://play.deskcine.com/?key=cc20112";
    //windows
    
    public static void main(String[] args){
        load_conf();
        //
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("DeskCine");
        shell.setSize(1140, 610);
        Menu bar = new Menu (shell, SWT.BAR);
	shell.setMenuBar (bar);
	MenuItem fileItem = new MenuItem (bar, SWT.CASCADE);
	fileItem.setText ("&Categorias");


        browser = new Browser(shell, SWT.NONE);
        browser.setBounds(0,0, shell.getSize().x, shell.getSize().y);
        browser.addLocationListener(new org.eclipse.swt.browser.LocationListener() {

            @Override
            public void changing(LocationEvent le) {
                if(le.location.contains("dc://"))
                {
                    loaddcp(le.location);
                }
            }

            @Override
            public void changed(LocationEvent le) {
                //System.out.println("2"+le.location);
            }

        });
        //
                //
        shell.addListener (SWT.Resize,  new Listener () {
        @Override
        public void handleEvent (Event e) {
          browser.setBounds(0,0, shell.getSize().x, shell.getSize().y);     
        }
        });
        //
        shell.open();
        navigate(home);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
        System.exit(0);
    }
      
    private static void loaddcp(String location) {
        if(location.endsWith("fav"))
        {
            //favoritos.main(null);
            //favoritos.setVisible(true);
        }
        else if(location.endsWith("config"))
        {
            System.out.println("Configurações");
        }
        else if(location.endsWith("home"))
        {
            browser.setUrl("http://localhost:3456/home");
        }
        browser.stop();
    }
    
    private static void load_conf(){
        main_config.save("main config");
        web_config.save("web config");
        //
        //Update_check();
    }
    
    private static void Update_check(){
        SimpleDateFormat formatador = new SimpleDateFormat("yyyyMMdd");
        String data = formatador.format(new Date());
        String mu = main_config.get("last_update");
        String wu = web_config.get("last_update");
        if(!wu.equals(mu))
        {
            try {
                Runtime.getRuntime().exec("java -jar DeskCine.jar updater");
            } catch (IOException ex) {
                Logger.getLogger(Start.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public static void navigate(String url) {
        browser.setUrl(url);
    }

}
